package studio7Omega.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import studio7Omega.excepcion.DAOExcepcion;
import studio7Omega.modelo.Local;
import studio7Omega.util.ConexionBD;

public class LocalDAO extends BaseDAO {

    public static Collection<Local> obtenerLocal() throws DAOExcepcion {
        String query = "select idLocal, descripcion, direccion, distrito, idusuario from Local  ";

        Collection<Local> lista = new ArrayList<Local>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = ConexionBD.obtenerConexion();
            stmt = con.prepareStatement(query);
            rs = stmt.executeQuery();
            Local va;
            while (rs.next()) {
                va = new Local();
                va.setIdLocal(rs.getInt("idLocal"));
                va.setDireccion(rs.getString("direccion"));
                va.setDescripcion(rs.getString("descripcion"));
                lista.add(va);
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            throw new DAOExcepcion(e.getMessage());
        } finally {
            try {
                rs.close();
                stmt.close();
                con.close();
            } catch (SQLException ex) {
                System.out.println("Error - " + ex.getMessage());
            }
        }
        return lista;
    }
}
